package m.jclecai.com.fragment;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;

import com.alibaba.fastjson.JSONArray;
import com.google.gson.Gson;
import com.squareup.okhttp.Request;

import java.io.IOException;

import butterknife.BindView;
import butterknife.OnClick;
import m.jclecai.com.MyApplication;
import m.jclecai.com.R;
import m.jclecai.com.activity.RmContentListActivity;
import m.jclecai.com.activity.RmDetailActivity;
import m.jclecai.com.activity.RmFamousActivity;
import m.jclecai.com.activity.RmListActivity;
import m.jclecai.com.adapter.RecomPersonAdapter;
import m.jclecai.com.adapter.RecomfamousAdapter;
import m.jclecai.com.base.BaseFragment;
import m.jclecai.com.constant.Constant_Url;
import m.jclecai.com.data.Lecai_SDK;
import m.jclecai.com.data.ReceivedData;
import m.jclecai.com.listener.Callback;
import m.jclecai.com.utils.http.HttpManager;
import m.jclecai.com.view.AutoSwipeRefreshLayout;

/**
 * Created by ZD on 2017/7/19.
 * 名家解说
 */

public class RecommendFragment extends BaseFragment {

    @BindView(R.id.rv_person)
    RecyclerView rvPerson;
    @BindView(R.id.rv_recommand)
    RecyclerView rvRecommand;
    @BindView(R.id.sr_refresh)
    AutoSwipeRefreshLayout srRefresh;
    RecomPersonAdapter rpAdapter;
    RecomfamousAdapter rfAdapter;
    JSONArray persion, recommed;
    String TAG = "RecommendFragment";
    /**
     * 名人列表
     */
    ReceivedData.ReconmentPersonData reconmentPersonData = null;
    /**
     * 推荐列表
     */
    ReceivedData.ReconmentContentData reconmentContentData = null;

    public enum JUMP {
        RmDetailActivity, RmFamousActivity, RmListActivity
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 12345678:
                    GridLayoutManager gManager = new GridLayoutManager(getActivity(), 4);
                    gManager.setOrientation(LinearLayoutManager.VERTICAL);
                    rvPerson.setLayoutManager(gManager);
                    rpAdapter = new RecomPersonAdapter(getActivity(), reconmentPersonData.records);
                    rpAdapter.setmCallback(callback);
                    rvPerson.setAdapter(rpAdapter);
                    break;

                case 12345679:
                    LinearLayoutManager lManager = new LinearLayoutManager(getActivity());
                    lManager.setOrientation(LinearLayoutManager.VERTICAL);
                    rvRecommand.setLayoutManager(lManager);
                    rfAdapter = new RecomfamousAdapter(getActivity(), reconmentContentData.records);
                    rfAdapter.setmCallback(callback);
                    rvRecommand.setAdapter(rfAdapter);
                    break;

            }
        }
    };

    @Override
    public int getLayoutId() {
        return R.layout.fragment_recommend;
    }

    @Override
    public void initView() {
        super.initView();
        setTitle("名家解说");
        persion = JSONArray.parseArray(personData);
        recommed = JSONArray.parseArray(recommData);
//        rightOptionText("", R.mipmap.jc_menu);
        sendReq(Constant_Url.APP_RECOMMEND_PERSON_TAG);
        sendReq(Constant_Url.APP_RECOMMEND_TAG);

        srRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                sendReq(Constant_Url.APP_RECOMMEND_PERSON_TAG);
                sendReq(Constant_Url.APP_RECOMMEND_TAG);
            }
        });
    }

    Callback callback = new Callback() {
        @Override
        public void callback(Object o) {
            super.callback(o);
            if (o.equals(JUMP.RmDetailActivity)) {
                startActivity(new Intent(getActivity(), RmDetailActivity.class));
            } else if (o.equals(JUMP.RmFamousActivity)) {
                startActivity(new Intent(getActivity(), RmFamousActivity.class));
            } else if (o.equals(JUMP.RmListActivity)) {
                startActivity(new Intent(getActivity(), RmListActivity.class));
            }
        }
    };
    //名人
    String personData = "[{\"allnum\":\"7\",\"jtype\":\"0\",\"authdescription\":\"小马哥从事足彩实战研究17年，善于从盘口及赔率的变化中把握庄家的意图，信奉足彩就是一个人的修行，公欲善其事，必先修其行！\",\"authName\":\"小马哥足彩推荐（社区）\",\"lznum\":\"2\",\"authheadImlUrl\":\"http://m.aicaicdn.com/upload/memberphoto/472/tn_1479291908097_160_160.jpg\",\"explans\":\"1\",\"authTag\":\"社区名人\",\"authsummary\":\"\",\"hitnum\":\"6\",\"authadvantage\":\"英超,西甲,德甲\"},{\"allnum\":\"7\",\"jtype\":\"0\",\"authdescription\":\"近10年中国各大门户网站竞彩推荐专家，有强大的基本面知识。4年前改用笔名黑白子，最擅长欧赔百家数据模型推荐，整体胜率非常高。\",\"authName\":\"必发黑白子\",\"lznum\":\"5\",\"authheadImlUrl\":\"http://m.aicaicdn.com/upload/memberphoto/287/tn_1492410956688_160_160.jpg\",\"explans\":\"1\",\"authTag\":\"彩票分析师\",\"authsummary\":\"\",\"hitnum\":\"6\",\"authadvantage\":\"英超,德甲,意甲\"},{\"allnum\":\"7\",\"jtype\":\"1\",\"authdescription\":\"曾在《新快报》担任编辑工作接近十年，长期关注各国篮球比赛。多年跟踪报道广东宏远，深圳马可波罗（东莞新世纪），佛山龙狮等本土俱乐部。无论是NBA，WNBA，CBA还是欧洲篮球，均拥有多年的从业经验。对各队内部资料了如指掌，丰富的经验+专业的眼光，将为广大球迷提供最独到的分析与意见。\",\"authName\":\"朱威\",\"lznum\":\"5\",\"authheadImlUrl\":\"http://m.aicaicdn.com/upload/memberphoto/6/tn_1476430804800_160_160.jpg\",\"explans\":\"0\",\"authTag\":\"媒体名记\",\"authsummary\":\"\",\"hitnum\":\"6\",\"authadvantage\":\"NBA,WNBA\"},{\"allnum\":\"7\",\"jtype\":\"0\",\"authdescription\":\"资深著名足彩自媒体，有最详实的基本面结合对盘口的理解带来最有参考意义的分析推荐。每逢焦点战必有独到见解，一起买球填肚子。\",\"authName\":\"买球填肚子\",\"lznum\":\"1\",\"authheadImlUrl\":\"http://m.aicaicdn.com/upload/memberphoto/802/tn_1498642815862_160_160.jpg\",\"explans\":\"0\",\"authTag\":\"专业玩家\",\"authsummary\":\"\",\"hitnum\":\"6\",\"authadvantage\":\"英超,德甲,日职\"},{\"allnum\":\"7\",\"jtype\":\"0\",\"authdescription\":\"资深职业媒体人，对亚盘变化十分敏感，根据球队实力、战意、状态等基本面数据，结合亚盘来分析比赛结果，喜欢稳串保本的情况下博高赔。\",\"authName\":\"老魏\",\"lznum\":\"0\",\"authheadImlUrl\":\"http://m.aicaicdn.com/upload/memberphoto/319/tn_1495616479919_160_160.jpg\",\"explans\":\"0\",\"authTag\":\"专业玩家\",\"authsummary\":\"\",\"hitnum\":\"6\",\"authadvantage\":\"英超,德甲,日乙\"},{\"allnum\":\"7\",\"jtype\":\"0\",\"authdescription\":\"足彩310长期合作专栏专家，足彩业内人士，多年来在工作中积累了丰富的足彩经验，对欧洲，亚洲，南美主流联赛有个人认识和理解，足彩已经成为生活的一部分；对主流彩票公司的操盘手法有深刻了解，从基本面出发，结合亚盘，欧赔数据变化分析比赛走势，期待与您共赢！\",\"authName\":\"吴可荐彩\",\"lznum\":\"5\",\"authheadImlUrl\":\"http://m.aicaicdn.com/upload/memberphoto/931/tn_1492506381027_160_160.jpg\",\"explans\":\"0\",\"authTag\":\"彩票分析师\",\"authsummary\":\"\",\"hitnum\":\"6\",\"authadvantage\":\"英超,法甲,日职\"},{\"allnum\":\"7\",\"jtype\":\"0\",\"authdescription\":\"前国脚，央视足球解说顾问，著名足球评论员\",\"authName\":\"徐阳\",\"lznum\":\"0\",\"authheadImlUrl\":\"http://m.aicaicdn.com/upload/memberphoto/223/tn_1465561207762_160_160.jpg\",\"explans\":\"0\",\"authTag\":\"TV大咖\",\"authsummary\":\"\",\"hitnum\":\"5\",\"authadvantage\":\"英超,西甲,德甲\"},{\"allnum\":\"7\",\"jtype\":\"0\",\"authdescription\":\"人气自媒体《小林话舍》主笔，小林最专注中超意甲，特别对意甲有独到见解且命中率居高不下！目前是天天盈球签约彩票分析师，曾为多家门户网站专题供稿！\",\"authName\":\"小林话舍\",\"lznum\":\"1\",\"authheadImlUrl\":\"http://m.aicaicdn.com/upload/memberphoto/823/tn_1476430319255_160_160.jpg\",\"explans\":\"0\",\"authTag\":\"彩票分析师\",\"authsummary\":\"\",\"hitnum\":\"5\",\"authadvantage\":\"意甲,中超\"},{\"allnum\":\"7\",\"jtype\":\"0\",\"authdescription\":\"彩易开盘团队是由多位前竞彩官方核心操盘手组成，曾经分别负责制定竞彩的日职，美职，法甲，巴西，南美，亚冠等联赛赔率，并负责中超的调研和模拟运营工作。团队赛事信息来源于香港马会和竞彩官方资讯提供商Soccer international，开盘和数据加工工作主要由操盘手负责，该团队同时在境外设有分支机构，由多位世界知名博彩公司操盘手组成，并将整个团队的开盘结果用于真实投注。\",\"authName\":\"彩易开盘团队\",\"lznum\":\"0\",\"authheadImlUrl\":\"http://m.aicaicdn.com/upload/memberphoto/873/tn_1468551535993_160_160.jpg\",\"explans\":\"0\",\"authTag\":\"彩票分析师\",\"authsummary\":\"\",\"hitnum\":\"5\",\"authadvantage\":\"瑞超,日职,日乙\"},{\"allnum\":\"7\",\"jtype\":\"0\",\"authdescription\":\"亚盘分析师，多年博彩公司操盘经验。针对主流博彩公司的初盘对比，跟踪即时盘口水位走势判断上下盘冷热给出投注建议。\",\"authName\":\"亚盘机器\",\"lznum\":\"0\",\"authheadImlUrl\":\"http://m.aicaicdn.com/upload/memberphoto/524/tn_1466737980251_160_160.jpg\",\"explans\":\"0\",\"authTag\":\"彩票分析师\",\"authsummary\":\"\",\"hitnum\":\"5\",\"authadvantage\":\"瑞超,日职,巴甲\"}]";
    String recommData = "[{\"endtime\":\"2017-07-22 02:30:00\",\"begintime\":\"2017-07-22 00:30:00\",\"authName\":\"小马哥足彩推荐（社区）\",\"lznum\":\"2\",\"authheadImlUrl\":\"http://m.aicaicdn.com/upload/memberphoto/472/tn_1479291908097_160_160.jpg\",\"authTag\":\"社区名人\",\"authsummary\":\"\",\"salepeice\":\"38\",\"timeType\":\"1\",\"lotterytype\":\"1\",\"isSee\":\"0\",\"title\":\"昨日2场全红！近6中5！小马哥今日推荐（1）：安郅vs阿姆卡尔\",\"allnum\":\"7\",\"jtype\":\"0\",\"authdescription\":\"小马哥从事足彩实战研究17年，善于从盘口及赔率的变化中把握庄家的意图，信奉足彩就是一个人的修行，公欲善其事，必先修其行！\",\"dbno\":\"2017072188124\",\"hitnum\":\"6\"},{\"endtime\":\"2017-07-22 04:45:00\",\"begintime\":\"2017-07-21 23:59:00\",\"authName\":\"王志勇\",\"lznum\":\"0\",\"authheadImlUrl\":\"http://m.aicaicdn.com/upload/memberphoto/181/tn_1476431203447_160_160.jpg\",\"authTag\":\"专业玩家\",\"authsummary\":\"\",\"salepeice\":\"88\",\"timeType\":\"1\",\"lotterytype\":\"1\",\"isSee\":\"0\",\"title\":\"超级八场独盈解读直捣黄龙盈利击杀！志勇雙全八场独盈选择再加送早场1900焦点友赛意见\",\"allnum\":\"7\",\"jtype\":\"0\",\"authdescription\":\"红魔死忠，长期活跃于曼联球迷社区，并多次造访曼彻斯特及慕尼黑等多个欧洲足球\\u201c心脏\\u201d地区。对欧陆多国足坛动态了如指掌！先后担任《足球通》、《评球品足》等媒体足球板块主笔，足坛动态一手掌握。因其本身乃会计师出身，故擅长以数据分析更是一大\\u201c杀器\\u201d，保证精准无死角！\",\"dbno\":\"2017072188122\",\"hitnum\":\"3\"},{\"endtime\":\"2017-07-22 02:30:00\",\"begintime\":\"2017-07-22 00:30:00\",\"authName\":\"魏刚\",\"lznum\":\"1\",\"authheadImlUrl\":\"http://m.aicaicdn.com/upload/memberphoto/963/tn_1485347803650_160_160.jpg\",\"authTag\":\"媒体名记\",\"authsummary\":\"\",\"salepeice\":\"58\",\"timeType\":\"1\",\"lotterytype\":\"1\",\"isSee\":\"0\",\"title\":\"推1送1继续(1)!俄超莫斯科中央陆军VS莫斯科火车头+安郅VS阿姆卡尔2场打包\",\"allnum\":\"7\",\"jtype\":\"0\",\"authdescription\":\"国际足球专家，新浪2016欧洲杯策划人，意甲策划人\",\"dbno\":\"2017072188120\",\"hitnum\":\"4\"},{\"endtime\":\"2017-07-22 09:00:00\",\"begintime\":\"2017-07-22 07:00:00\",\"authName\":\"戴维\",\"lznum\":\"0\",\"authheadImlUrl\":\"http://m.aicaicdn.com/upload/memberphoto/285/tn_1496825975402_160_160.jpg\",\"authTag\":\"彩票分析师\",\"authsummary\":\"\",\"salepeice\":\"58\",\"timeType\":\"1\",\"lotterytype\":\"1\",\"isSee\":\"0\",\"title\":\"周五【第三场 奥兰多城VS亚特兰大联】美职精选！\",\"allnum\":\"7\",\"jtype\":\"0\",\"authdescription\":\"资深足彩专家，凯利指数最早使用者，对各博彩公司盘口赔率特点十分熟悉，善于解读盘口异常、假盘和蛊惑盘，理论及实战经验丰富，胜率高而稳定。\",\"dbno\":\"2017072188118\",\"hitnum\":\"4\"},{\"endtime\":\"2017-07-22 04:45:00\",\"begintime\":\"2017-07-22 02:45:00\",\"authName\":\"戴维\",\"lznum\":\"0\",\"authheadImlUrl\":\"http://m.aicaicdn.com/upload/memberphoto/285/tn_1496825975402_160_160.jpg\",\"authTag\":\"彩票分析师\",\"authsummary\":\"\",\"salepeice\":\"58\",\"timeType\":\"1\",\"lotterytype\":\"1\",\"isSee\":\"0\",\"title\":\"周五【第二场 罗斯郡VS希伯尼安】苏格兰联赛杯精选！\",\"allnum\":\"7\",\"jtype\":\"0\",\"authdescription\":\"资深足彩专家，凯利指数最早使用者，对各博彩公司盘口赔率特点十分熟悉，善于解读盘口异常、假盘和蛊惑盘，理论及实战经验丰富，胜率高而稳定。\",\"dbno\":\"2017072188116\",\"hitnum\":\"4\"},{\"endtime\":\"2017-07-22 02:30:00\",\"begintime\":\"2017-07-22 00:30:00\",\"authName\":\"戴维\",\"lznum\":\"0\",\"authheadImlUrl\":\"http://m.aicaicdn.com/upload/memberphoto/285/tn_1496825975402_160_160.jpg\",\"authTag\":\"彩票分析师\",\"authsummary\":\"\",\"salepeice\":\"58\",\"timeType\":\"1\",\"lotterytype\":\"1\",\"isSee\":\"0\",\"title\":\"周五【第一场 安郅VS阿姆卡尔】俄超精选！\",\"allnum\":\"7\",\"jtype\":\"0\",\"authdescription\":\"资深足彩专家，凯利指数最早使用者，对各博彩公司盘口赔率特点十分熟悉，善于解读盘口异常、假盘和蛊惑盘，理论及实战经验丰富，胜率高而稳定。\",\"dbno\":\"2017072188114\",\"hitnum\":\"4\"},{\"endtime\":\"2017-07-22 02:30:00\",\"begintime\":\"2017-07-22 00:30:00\",\"authName\":\"必发黑白子\",\"lznum\":\"5\",\"authheadImlUrl\":\"http://m.aicaicdn.com/upload/memberphoto/287/tn_1492410956688_160_160.jpg\",\"authTag\":\"彩票分析师\",\"authsummary\":\"\",\"salepeice\":\"58\",\"timeType\":\"1\",\"lotterytype\":\"1\",\"isSee\":\"0\",\"title\":\"昨天2串1唯一推荐单选红单！（看今天俄超2串1过关推荐！）信心还是强！\",\"allnum\":\"7\",\"jtype\":\"0\",\"authdescription\":\"近10年中国各大门户网站竞彩推荐专家，有强大的基本面知识。4年前改用笔名黑白子，最擅长欧赔百家数据模型推荐，整体胜率非常高。\",\"dbno\":\"2017072188112\",\"hitnum\":\"6\"},{\"endtime\":\"2017-07-21 12:30:00\",\"begintime\":\"2017-07-21 10:30:00\",\"authName\":\"林勇\",\"lznum\":\"3\",\"authheadImlUrl\":\"http://m.aicaicdn.com/upload/memberphoto/286/tn_1477378353722_160_160.jpg\",\"authTag\":\"媒体名记\",\"authsummary\":\"\",\"salepeice\":\"88\",\"timeType\":\"2\",\"lotterytype\":\"1\",\"isSee\":\"0\",\"title\":\"金杯赛临场！周五上午10:30推荐：墨西哥 VS 洪都拉斯\",\"allnum\":\"7\",\"jtype\":\"0\",\"authdescription\":\"第二批国家体彩中心颁发国家足彩培训师，2003年进入足彩行业至今，时任彩报《赢盘》编辑主任，现任5U体育内容总监、电台节目主持人。时至今日，一直担任广东地区足彩、足球评论类电台节目主持和嘉宾，曾任欧洲足球频道西甲解说嘉宾。一直专注于单场足彩领域，对英超、西甲、德甲、欧冠及北欧联赛等赛事有多年推介经验，对NBA、篮球欧冠等赛事也有涉猎。\",\"dbno\":\"2017072188110\",\"hitnum\":\"4\"},{\"endtime\":\"2017-07-21 11:30:00\",\"begintime\":\"2017-07-21 09:30:00\",\"authName\":\"林勇\",\"lznum\":\"3\",\"authheadImlUrl\":\"http://m.aicaicdn.com/upload/memberphoto/286/tn_1477378353722_160_160.jpg\",\"authTag\":\"媒体名记\",\"authsummary\":\"\",\"salepeice\":\"88\",\"timeType\":\"2\",\"lotterytype\":\"1\",\"isSee\":\"0\",\"title\":\"吉尼斯杯临场！周五上午09:30推荐：曼联 VS 曼城\",\"allnum\":\"7\",\"jtype\":\"0\",\"authdescription\":\"第二批国家体彩中心颁发国家足彩培训师，2003年进入足彩行业至今，时任彩报《赢盘》编辑主任，现任5U体育内容总监、电台节目主持人。时至今日，一直担任广东地区足彩、足球评论类电台节目主持和嘉宾，曾任欧洲足球频道西甲解说嘉宾。一直专注于单场足彩领域，对英超、西甲、德甲、欧冠及北欧联赛等赛事有多年推介经验，对NBA、篮球欧冠等赛事也有涉猎。\",\"dbno\":\"2017072188108\",\"hitnum\":\"4\"},{\"endtime\":\"2017-07-21 12:30:00\",\"begintime\":\"2017-07-21 09:30:00\",\"authName\":\"剑叔jingcai（社区）\",\"lznum\":\"2\",\"authheadImlUrl\":\"http://m.aicaicdn.com/upload/memberphoto/480/tn_1479291876857_160_160.jpg\",\"authTag\":\"社区名人\",\"authsummary\":\"\",\"salepeice\":\"38\",\"timeType\":\"2\",\"lotterytype\":\"1\",\"isSee\":\"0\",\"title\":\"剑叔7月20日竞彩场次026、027组合\",\"allnum\":\"7\",\"jtype\":\"0\",\"authdescription\":\"社区超级红人，足彩、竞彩爱好者，十多年资深彩民，擅长基本面与欧亚盘口的关联对比研究。\",\"dbno\":\"2017072188106\",\"hitnum\":\"5\"}]";


    private String Url = "";

    /**
     * 解说数据请求
     */
    private void sendReq(final int tag) {
        Constant_Url.HTTP_URL = MyApplication.HTTP_URL_RECOMMEND;
        if (tag == Constant_Url.APP_RECOMMEND_PERSON_TAG) {
            Url = Constant_Url.APP_RECOMMEND_PERSON_URL;
        } else if (tag == Constant_Url.APP_RECOMMEND_TAG) {
            Url = Constant_Url.APP_RECOMMEND_URL;
        }
        showDialog();
        HttpManager.getAsyn(Url, new HttpManager.StringCallback() {

            @Override
            public void onFailure(Request request, IOException e) {
                dissDialog();
                srRefresh.setRefreshing(false);
                DisPlay("网络异常！！！");
            }

            @Override
            public void onResponse(String response) {
                dissDialog();
                srRefresh.setRefreshing(false);
                if (!TextUtils.isEmpty(response)) {
                    Gson gson = new Gson();
                    if (tag == Constant_Url.APP_RECOMMEND_PERSON_TAG) {
                        reconmentPersonData = gson.fromJson(response, ReceivedData.ReconmentPersonData.class);
                        Lecai_SDK.reconmentPersonData = reconmentPersonData;
                        Message msg = new Message();
                        msg.what = 12345678;
                        handler.sendMessage(msg);
                    } else if (tag == Constant_Url.APP_RECOMMEND_TAG) {
                        reconmentContentData = gson.fromJson(response, ReceivedData.ReconmentContentData.class);
                        Message msg = new Message();
                        msg.what = 12345679;
                        handler.sendMessage(msg);
                    }
                }
            }
        });

    }

    @OnClick(R.id.tv_loadall)
    public void onClick(View view) {
        startActivity(new Intent(getActivity(), RmContentListActivity.class));
    }


}
